package files;

import io.Configuration;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import exceptions.CouldNotRenameException;
import exceptions.NotAFolderException;

public class Library {

	private Map<String, File> lib;
	private long browsingTime;

	public Library() {
		Date d = new Date();
		browsingTime = d.getTime();
		lib = new HashMap<String, File>();
	}
	
	public void updateTime() {
		Date d = new Date();
		browsingTime = d.getTime();
	}
	
	public long getBrowsingTime() {
		return browsingTime;
	}
	
	public void addFolder(String path) throws NotAFolderException {
		File dir = new File(path);
		if(!dir.isDirectory() || !dir.exists())
			throw new NotAFolderException();
		else
			lib.put(path, dir);
	}
	
	/**
	 * List all files in the Library.
	 */
	public void listFiles() {
		for(Entry<String, File> entry : lib.entrySet()) {
			File file = entry.getValue();
			File[] childs = file.listFiles();
			for(int i=0; i<childs.length; i++)
				System.out.println(childs[i].getName()+"\t"+childs[i].length()+"b\t"+childs[i].lastModified());
		}
	}
	
	/**
	 * List only files filtered.
	 * @param extensions
	 */
	public File[] list(final String[] extensions) {
		File[] children = null;
		Pattern pattern;
		Matcher matcher;
		
		for(Entry<String, File> entry : lib.entrySet()) {
			File file = entry.getValue();
			
			// selectionne et filtre les fichiers
			children = file.listFiles(new FilenameFilter() {
			    public boolean accept(File directory, String fileName) {
			    	boolean accepted = false;
			    	for(int i=0; i<extensions.length; i++)
			    			accepted = accepted || fileName.toLowerCase().endsWith(extensions[i]);
			        return accepted;
			    }
			});

			/*pattern = Pattern.compile("^(\\d{8})(.jpg)$");
			for(int i=0; i<childs.length; i++) {
				matcher = pattern.matcher(children[i]);
				if(matcher.find())
					System.out.println(children[i]);
				//System.out.println(children[i]+"\t"+children[i].length()+"b");
			}*/
		}
		
		return children;
	}
	
	public void close() {
		this.updateTime();
		Configuration.save(this);
	}
	
	// list new files (avec un filtre >date)

	public void testing() {
		System.out.println("Name\tExec\tRead\tWrite");
		for(Entry<String, File> entry : lib.entrySet()) {
			//String name = entry.getKey();
			File file = entry.getValue();
			//System.out.println(file.getName()+"\t"+file.canExecute()+"\t"+file.canRead()+"\t"+file.canWrite()+"\t"+file.getTotalSpace());
			//this.listFiles();
			this.list(new String[]{"jpg"});
		}
	}

	// isnew?
	
	public void renameJPEG() throws CouldNotRenameException {
	Pattern pattern;
	Matcher matcher;
		
		pattern = Pattern.compile("jpeg$");
		for(File f : list(new String[]{"jpeg"})) {
			matcher = pattern.matcher(f.getName().toLowerCase());
			if(!f.renameTo(new File(f.getParent()+"\\"+matcher.replaceAll("jpg"))))
				throw new CouldNotRenameException();
		}
	}
	
	public static void main(String[] args) {
		Library lib = new Library();
		try {
			lib.addFolder("C:\\test");
		} catch (NotAFolderException e) {
			e.printStackTrace();
		}

		//lib.testing();
		try {
			lib.renameJPEG();
		} catch (CouldNotRenameException e) {
			e.printStackTrace();
		}
		lib.close();
	}

}
